{% load humanize widget_tweaks %}

<div class="row">
    <div class="col-12 table-responsive">
        <table id="scroll-horizontal-datatable"
               class="table table-hover table-bordered table-centered table-condensed table-sm w-100 m-0 nowrap">
            <thead>
            <tr class="text-center bg-dragula">
                <th>수납일자</th>
                <th>납부회차</th>
                <th>수납금액</th>
                <th>수납계좌</th>
                <th>입금자명</th>
            </tr>
            </thead>
            <tbody>
            {% for pay in cont_payments %}
                <tr style="background: #F9FAD9;">
                    <td class="text-center">{{ pay.deal_date|date:"Y-m-d"|default:"-" }}</td>
                    <td class="text-center">{{ pay.installment_order|default:"-" }}</td>
                    <td class="text-right">
                        <a href="{% url 'rebs:cash-inout:payment-register' %}?project={{ this_project.id }}&type={{ request.GET.type }}&contract={{ request.GET.contract }}&payment_id=
                                {{ pay.id }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}"
                           class="{% if pay.project_account_d2.id >= 62 %}text-danger{% endif %}">
                            {{ pay.income|default:"-"|intcomma }}
                        </a>
                    </td>
                    <td class="text-center">{{ pay.bank_account|default:"-" }}</td>
                    <td class="text-center">{{ pay.trader|default:"-" }}</td>
                </tr>
            {% endfor %}
            <tr class="bg-white">
                <th class="text-center bg-light">합계</th>
                <th></th>
                <th class="text-right text-dark">{{ payment_sum.income__sum|default:"-"|intcomma }}</th>
                <th></th>
                <th></th>
            </tr>
            </tbody>
        </table>
    </div>

    <form action="" method="POST" class="col-12 mt-3 form-horizontal needs-validation"
          novalidate>
        {% csrf_token %}
        {{ form.project }}
        {{ form.project_account_d1 }}
        {{ form.project_account_d2 }}
        {{ form.contract }}
        {{ form.errors }}
        {% if not request.GET.delete %}
            <div class="form-group row border-top p-1 mb-0">
                <label for="{{ form.deal_date.id_for_label }}"
                       class="col-3 col-sm-2 col-form-label bg-light text-center">
                    수납일자
                </label>
                <div class="col-9 col-sm-4">
                    {{ form.deal_date|add_class:"form-control"|attr:"placeholder:수납일자"|attr:"data-provide=datepicker data-date-format=yyyy-mm-dd data-date-autoclose=true data-toggle=input-mask data-mask-format=0000-00-00" }}
                </div>
                <div class="d-none d-sm-block col-6"></div>
            </div>

            <div class="form-group row border-top p-1 mb-0">
                <label for="{{ form.installment_order.id_for_label }}"
                       class="col-3 col-sm-2 col-form-label bg-light text-center">
                    {{ form.installment_order.label }}
                </label>
                <div class="col-9 col-sm-4">
                    {{ form.installment_order|add_class:"form-control"|attr:"required" }}
                </div>

                <label for="{{ form.income.id_for_label }}"
                       class="col-3 col-sm-2 col-form-label bg-light text-center">
                    수납금액
                </label>
                <div class="col-9 col-sm-4">
                    {{ form.income|add_class:"form-control"|attr:"placeholder:수납금액"|attr:"required" }}
                </div>
            </div>

            <div class="form-group row border-top p-1 mb-0">
                <label for="{{ form.bank_account.id_for_label }}"
                       class="col-3 col-sm-2 col-form-label bg-light text-center">
                    {{ form.bank_account.label }}
                </label>
                <div class="col-9 col-sm-4">
                    {{ form.bank_account|add_class:"form-control" }}
                </div>

                <label for="{{ form.trader.id_for_label }}"
                       class="col-3 col-sm-2 col-form-label bg-light text-center">
                    입금자명
                </label>
                <div class="col-9 col-sm-4">
                    {{ form.trader|add_class:"form-control"|attr:"placeholder:입금자명"|attr:"required" }}
                </div>
            </div>

            <div class="form-group row border-top border-bottom p-1 mb-0">
                <label for="{{ form.bank_account.id_for_label }}"
                       class="col-3 col-sm-2 col-form-label bg-light text-center">
                    {{ form.note.label }}
                </label>
                <div class="col-9 col-sm-10">
                    {{ form.note|add_class:"form-control"|attr:"placeholder:기타 특이사항"|attr:"rows:3" }}
                </div>
            </div>

            <div class="form-group row border-top border-bottom p-2 mt-3 bg-dragula justify-content-end">
                {% if request.user.is_superuser or request.user.staffauth.payment > '1' %}
                    {% if request.GET.payment_id %}
                        <button type="button" class="btn btn-light mr-1"
                                onclick="location.href='{{ request.path }}?project={{ request.GET.project }}&type={{ request.GET.type }}&contract={{ request.GET.contract }}'">
                            뒤로가기
                        </button>
                        <button type="button" class="btn btn-danger mr-1"
                                onclick="location.href='{{ request.path }}?project={{ request.GET.project }}&type={{ request.GET.type }}&contract={{ request.GET.contract }}&payment_id={{ request.GET.payment_id }}&delete=ok'">
                            삭제하기
                        </button>
                    {% endif %}
                    <button class="btn btn-{% if not request.GET.payment_id %}primary{% else %}success{% endif %}"
                            {% if not request.GET.contract %}type="button"
                            onclick="alert('입력할 계약을 선택하십시요.')"{% endif %}>
                        {% if not request.GET.payment_id %}신규등록{% else %}변경등록{% endif %}
                    </button>
                {% else %}
                    <button type="button" class="btn btn-outline-secondary"
                            onclick="alert('건별 수납 관리 권한이 없습니다. 관리자에게 문의하여 주십시요.')">조회권한 사용자
                    </button>
                {% endif %}
            </div>
        {% else %}
            <div class="row p-4 justify-content-center">
                <div class="col-12">납입일자 : {{ this_payment.deal_date }}</div>
                <div class="col-12">입금금액 : {{ this_payment.income|intcomma }}원</div>
                <div class="col-12">납입회차 : {{ this_payment.installment_order }}</div>
                <div class="col-12 mb-3">입 금 자 : {{ this_payment.trader }}</div>

                <p class="mb-3 col-12 mb-4 text-danger">삭제후 복구할 수 없습니다. 해당 거래 건을 삭제하시겠습니까?</p>
            </div>

            <div class="form-group row border-top border-bottom p-2 mt-3 bg-dragula justify-content-end">
                {% if request.user.is_superuser or request.user.staffauth.payment > '1' %}
                    <button type="button" class="btn btn-light ml-1"
                            onclick="location.href='{{ request.path }}?project={{ request.GET.project }}&type={{ request.GET.type }}&contract={{ request.GET.contract }}&payment_id={{ request.GET.payment_id }}'">
                        뒤로가기
                    </button>
                    <button type="button" class="btn btn-danger"
                            onclick="location.href='{% url 'rebs:cash-inout:payment-delete' request.GET.payment_id %}?project={{ request.GET.project }}&type={{ request.GET.type }}&contract={{ request.GET.contract }}&payment_id={{ request.GET.payment_id }}'">
                        삭제확인
                    </button>
                {% else %}
                    <button type="button" class="btn btn-outline-secondary"
                            onclick="alert('건별 수납 관리 권한이 없습니다. 관리자에게 문의하여 주십시요.')">조회권한 사용자
                    </button>
                {% endif %}
            </div>
        {% endif %}
    </form>
</div>
